// pages/creat-order/index.js

 var zUrl = getApp().globalData.zUrl;
Page({

  /**
   * 页面的初始数据
   */
 
  data: {
    esc:'',
    token: '',
    tan:2,
    is_esc:true,
    rideoImg: '/images/ground.png',
    rideoImgC:'/images/ground-gray.png',
    red:'red',
    gray:'gray',
    creatType:0,
    adviser:''
  },
  tanchuang: function (e) {
    console.log()
    if (this.data.creatTel == null) {
      wx.showToast({
        title: '认购人手机不能为空',
        icon: "none",
        duration: 2000
      })
    } else if (this.data.creatName == null) {
      wx.showToast({
        title: '认购人姓名不能为空',
        icon: "none",
        duration: 2000
      })
    } else if (this.data.creatHouse == null) {
      wx.showToast({
        title: '认购户型不能为空',
        icon: "none",
        duration: 2000
      })
    } else if (this.data.creatArea == null) {
      wx.showToast({
        title: '户型面积不能为空',
        icon: "none",
        duration: 2000
      })
    } else if (this.data.creatPrice == null) {
      wx.showToast({
        title: '房款总价不能为空',
        icon: "none",
        duration: 2000
      })
    }else{
      this.setData({
        tan: 1
      })
    }
   
  },
  close: function (e) {
    this.setData({
      tan: 2
    })
  },
  creatTel: function (e){
    var that = this
 
      that.setData({
        creatTel: e.detail.value,
        creatTelFinsh: e.detail.cursor
      })
    
      if (e.detail.cursor == '11') {
        
       // console.log(that.data.token)
        wx.request({
          url: zUrl+'/api/order/usercoupon',
          header: {
            'Cookie': 'laravel_session=' + wx.getStorageSync('laravel_session'),
            Accept: 'application/json',
            Authorization: 'Bearer ' + wx.getStorageSync('token')
          },
          data: {
            sub_tel: that.data.creatTel
          },
          method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          //预防乱码的不用管
          success: function (res) {
            wx.hideLoading()
            //处理返回的数据
            if(res.data.code==200){
              that.setData({
                esc: res.data.data.esc,
                esc_id: res.data.data.id,
                is_esc: false
              })
            }else{
              wx.showToast({
                title: res.data.message,
                icon: "none",
                duration: 2000
              })
            }
          },
          fail: function (error) {

          },
          complete: function (res) {

          }
        });
      }
    
  
  },
  creatName: function (e) {
    
    this.setData({
      creatName: e.detail.value,
    })
  },
   creatHouse: function (e) {
      this.setData({
        creatHouse: e.detail.value,
  })
  },
creatArea: function (e) {
      this.setData({
        creatArea: e.detail.value,
  })
},
   creatPrice: function (e) {
      this.setData({
        creatPrice: e.detail.value,
  })
   },
  creatSales: function (e) {
      this.setData({
        creatSales: e.detail.value,
  })
  },
  creatNote: function (e) {
      this.setData({
        creatNote: e.detail.value,
  })
  },
  noSales:function(){
    this.setData({
      rideoImg: '/images/ground.png',
      rideoImgC: '/images/ground-gray.png',
      red:'red',
      gray:'gray',
      creatType:0,
    })
  },
  hasSales(){
    this.setData({
      rideoImg:'/images/ground-gray.png',
      rideoImgC:'/images/ground.png',
      gray:'red',
      red:'gray',
      creatType: 1,
    })
  },
  create(e){
     console.log(e.detail.formId);
    if (this.data.creatTel==null){
      wx.showToast({
        title: '认购人手机不能为空',
        icon: "none",
        duration: 2000
      })
    }else if (this.data.creatName == null) {
      wx.showToast({
        title: '认购人姓名不能为空',
        icon: "none",
        duration: 2000
      })
    } else if (this.data.creatHouse == null) {
      wx.showToast({
        title: '认购户型不能为空',
        icon: "none",
        duration: 2000
      })
    } else if (this.data.creatArea == null) {
      wx.showToast({
        title: '户型面积不能为空',
        icon: "none",
        duration: 2000
      })
    } else if (this.data.creatPrice == null) {
      wx.showToast({
        title: '房款总价不能为空',
        icon: "none",
        duration: 2000
      })
    }
    else{
      wx.request({
        url: zUrl+'/api/order/add',
        data: {
          sub_tel: this.data.creatTel,
          sub_name: this.data.creatName,
          sub_house_type: this.data.creatHouse,
          sub_house_area: this.data.creatArea,
          house_total_price: this.data.creatPrice,
          salesperson: this.data.adviser.account_name,
          u_c_id: this.data.esc_id,
          clearing_type: this.data.creatType,
          p_id:1,
          note: this.data.creatNote,
          formid: e.detail.formId
        },
        header: {
          'Cookie': 'laravel_session=' + wx.getStorageSync('laravel_session'),
          Accept: 'application/json',
          Authorization: 'Bearer ' + wx.getStorageSync('token')
        },
        method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        //预防乱码的不用管
        success: function (res) {
          wx.hideLoading()
          //console.log(res.data)//处理返回的数据
          if(res.data.code=='200'){
            wx.switchTab({
              url: '/pages/index/index'
            })
          }else{
            wx.showToast({
              title: res.data.message,
              icon: "none",
              duration: 2000
            })
          }
         

        },
        fail: function (error) {
          console.log(error)
          
        },
        complete: function (res) {

        }
      });
    }
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      adviser: wx.getStorageSync('adviser')
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})